package me.jiatao.paging2.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import me.jiatao.paging2.paging2.ProjectDataSourceFactory
import me.jiatao.paging2.pojo.ArticleItem

class ProjectViewModel : ViewModel() {
    private val pageSize = 20

    //PagedList配置
    private val config = PagedList.Config.Builder()
        .setInitialLoadSizeHint(pageSize) //设置首次加载的数量
        .setPageSize(pageSize) //设置每页加载的数量
        .setPrefetchDistance(10) //设置距离每页最后数据项来时预加载下一页数据
        .setEnablePlaceholders(false) //设置是否启用UI占用符
        .build()

    private val projectDataSourceFactory = ProjectDataSourceFactory()

    private val projectViewModel: LiveData<PagedList<ArticleItem>> =
        LivePagedListBuilder(projectDataSourceFactory, config).build()

    fun getProjectViewModel(): LiveData<PagedList<ArticleItem>> {
        return projectViewModel
    }
}